package cn.edu.nchu.fragmentdemo.myComponents.components;

import android.content.Context;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.PathEffect;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.Polyline;

import cn.edu.nchu.fragmentdemo.MainActivity;

public class MyPolylineClass extends Polyline {
    private MainActivity mainActivity;
    private Context context;
    private MapView mapView;
    MyMarkerClass distanceMarker;
    Polyline polyline;
    public MyMarkerClass myMarkerClass1;
    public MyMarkerClass myMarkerClass2;

    public MyPolylineClass() {
        mainActivity = (MainActivity) MainActivity.getActivity();
        //mainActivity = (MainActivity) ActivityStackManager.getInstance().getCurrentActivity();
        //context = mainActivity.getApplicationContext();
        mapView = mainActivity.getMap();
        distanceMarker = new MyMarkerClass(mapView);
        polyline = new Polyline();
    }

    public MyPolylineClass(MyMarkerClass myMarkerClass1, MyMarkerClass myMarkerClass2) {
        mainActivity = (MainActivity) MainActivity.getActivity();
        //context = mainActivity.getApplicationContext();
        mapView = mainActivity.getMap();
        distanceMarker = new MyMarkerClass(mapView);
        this.myMarkerClass1 = myMarkerClass1;
        this.myMarkerClass2 = myMarkerClass2;
        polyline = new Polyline();
    }

    public double showDistance(GeoPoint geoPoint1, GeoPoint geoPoint2) {
        if (geoPoint1.getLongitude() > geoPoint2.getLongitude()) {//交换
            GeoPoint temp = new GeoPoint(0, 0);
            temp = geoPoint1;
            geoPoint1 = geoPoint2;
            geoPoint2 = temp;
        }
        double len = 0;
        polyline = new Polyline();
        polyline.addPoint(geoPoint1);
        polyline.addPoint(geoPoint2);
        polyline.setWidth(5f);
        polyline.setColor(0xccFFFF00);
        len = polyline.getDistance();
        //小数点后两位
        len = (double) Math.round(len * 100) / 100;
        //设置marker
        double degree = geoPoint1.bearingTo(geoPoint2);
        //
        GeoPoint midGeoPiont = GeoPoint.fromCenterBetween(geoPoint1, geoPoint2);
        distanceMarker = new MyMarkerClass(mapView);
        distanceMarker.setTextLabelBackgroundColor(Color.TRANSPARENT);
        distanceMarker.setTextLabelForegroundColor(Color.WHITE);
        distanceMarker.setTextLabelFontSize(25);
        distanceMarker.setTextIcon("" + len + "米");
        distanceMarker.setAnchor(Marker.ANCHOR_LEFT, Marker.ANCHOR_TOP);
        distanceMarker.setPosition(midGeoPiont);
        distanceMarker.setRotation((float) (360 - degree + 90));
        distanceMarker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker, MapView mapView) {
                return false;
            }
        });
        mapView.getOverlays().add(distanceMarker);
        mapView.getOverlays().add(polyline);
        //mapView.getOverlays().remove(this);
        mapView.invalidate();
        return len;
    }

    public double showDistanceForObject() {
        double len = 0;
        GeoPoint geoPoint1 = myMarkerClass1.getPosition();
        GeoPoint geoPoint2 = myMarkerClass2.getPosition();
        if (geoPoint1.equals(geoPoint2)) {
            //
        } else {
            if (geoPoint1.getLongitude() > geoPoint2.getLongitude()) {//交换
                GeoPoint temp = new GeoPoint(0, 0);
                temp = geoPoint1;
                geoPoint1 = geoPoint2;
                geoPoint2 = temp;
            }
            polyline = new Polyline();
            polyline.setGeodesic(true);
            polyline.addPoint(geoPoint1);
            polyline.addPoint(geoPoint2);
            polyline.setWidth(5f);

            polyline.setColor(0xcc00FF00);
            len = polyline.getDistance();
            //小数点后两位
            len = (double) Math.round(len * 100) / 100;
            //设置marker
            double degree = geoPoint1.bearingTo(geoPoint2);
            //
            GeoPoint midGeoPiont = GeoPoint.fromCenterBetween(geoPoint1, geoPoint2);
            distanceMarker = new MyMarkerClass(mapView);
            distanceMarker.setTextLabelBackgroundColor(Color.TRANSPARENT);
            distanceMarker.setTextLabelForegroundColor(Color.WHITE);
            distanceMarker.setTextLabelFontSize(25);
            distanceMarker.setTextIcon("" + len + "米");
            distanceMarker.setAnchor(Marker.ANCHOR_LEFT, Marker.ANCHOR_TOP);
            distanceMarker.setPosition(midGeoPiont);
            distanceMarker.setRotation((float) (360 - degree + 90));
            distanceMarker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker, MapView mapView) {
                    return false;
                }
            });
            mapView.getOverlays().add(distanceMarker);
            mapView.getOverlays().add(polyline);
            //mapView.getOverlays().remove(this);
            mapView.invalidate();
        }
        return len;
    }

    /**
     * 添加实线区域
     */
    /*private Overlay addFullArea(List<GeoPoint> pointList, int color) {
        PathOverlay myPath = new PathOverlay(color, mainActivity);
        Paint paint = new Paint();
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setColor(color);
        //paint.setAntiAlias(true);
        paint.setPathEffect(null);

        myPath.setPaint(paint);
        for (GeoPoint geoPoint : pointList) {
            myPath.addPoint(geoPoint);
        }
        mapView.getOverlays().add(myPath);
        mapView.invalidate();
        return myPath;
    }*/

    /**
     * 添加虚线区域
     */
    /*private Overlay addDottedArea(List<GeoPoint> pointList) {
        PathOverlay myPath = new PathOverlay(Color.RED, mainActivity);
        PathEffect effect = null;
        //虚线
        effect = getDottedLine(30, 30);
        Paint paint = new Paint();
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setColor(Color.RED);
        //paint.setAntiAlias(true);
        paint.setPathEffect(effect);

        myPath.setPaint(paint);
        for (GeoPoint geoPoint : pointList) {
            myPath.addPoint(geoPoint);
        }
        mapView.getOverlays().add(myPath);
        return myPath;
    }*/

    /**
     * 获取虚线
     *
     * @param lineLength
     * @param space
     * @return
     */
    private PathEffect getDottedLine(int lineLength, int space) {
        //虚线
        DashPathEffect pee = new DashPathEffect(new float[]{lineLength, space}, 0);
        return pee;
    }

    public void clearOnMap() {
        mapView.getOverlays().remove(polyline);
        mapView.getOverlays().remove(distanceMarker);
        mapView.getOverlays().remove(this);
        mapView.invalidate();
    }

/*    @Override
    public boolean onLongPress(final MotionEvent e, final MapView mapView) {
        clearOnMap();
        //mapView.getOverlays().remove(this);
        mapView.invalidate();
        return super.onLongPress(e, mapView);
    }*/

}
